package com.cq.hd.product.controller;

import cn.hutool.core.bean.BeanUtil;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.ResultGenerator;
import com.cq.hd.product.api.TbActiveApi;
import com.cq.hd.product.api.dto.*;
import com.cq.hd.product.api.vo.*;
import com.cq.hd.product.po.TbActiveGroupBuyPo;
import com.cq.hd.product.service.TbActiveGroupBuyService;
import com.cq.hd.product.service.TbActiveService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 活动表 前端控制器
 * </p>
 *
 * @author mYunYu
 * @since 2023-11-23
 */
@RestController
public class TbActiveController implements TbActiveApi {

    @Autowired
    private TbActiveService activeService;
    @Autowired
    private TbActiveGroupBuyService activeGroupBuyService;

    @ApiOperation(value = "校验商品库存")
    @ApiImplicitParam(name = "activeGoodsStockCheckDto", value = "实体", paramType = "body", dataType = "activeGoodsStockCheckDto")
    @Override
    public Result<Object> checkGoodsStock(ActiveGoodsStockCheckDto activeGoodsStockCheckDto) {
        return ResultGenerator.success(activeService.checkGoodsStock(activeGoodsStockCheckDto));
    }

    @ApiOperation(value = "发布活动")
    @ApiImplicitParam(name = "activeReleaseDto", value = "实体", paramType = "body", dataType = "ActiveReleaseDto")
    @Override
    public Result<Object> releaseActive(@RequestBody ActiveReleaseDto activeReleaseDto) {
        return ResultGenerator.success(activeService.releaseActive(activeReleaseDto));
    }

    @Override
    public Result<Object> releaseGroupBuyActive(GroupBuyActiveReleaseDto activeReleaseDto) {
        return ResultGenerator.success(activeService.releaseActive(activeReleaseDto));
    }

    @ApiOperation(value = "我的活动列表")
    @ApiImplicitParam(name = "appMyActivePageDto", value = "实体", paramType = "query", dataType = "AppMyActivePageDto")
    @ApiResponse(response = AppMyActivePageVo.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<Page<AppMyActivePageVo>> pageMyActive(AppMyActivePageDto appMyActivePageDto) {
        return ResultGenerator.success(activeService.pageMyActive(appMyActivePageDto));
    }

    @ApiOperation(value = "修改活动")
    @ApiImplicitParam(name = "activeUpdateDto", value = "实体", paramType = "body", dataType = "ActiveUpdateDto")
    @Override
    public Result<Object> updateActive(@RequestBody ActiveUpdateDto activeUpdateDto) {
        return ResultGenerator.success(activeService.updateActive(activeUpdateDto));
    }

    @ApiOperation(value = "我的活动详情")
    @ApiImplicitParam(name = "id", value = "活动id", paramType = "query", dataType = "Long")
    @ApiResponse(response = AppMyActiveDetailsVo.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<AppMyActiveDetailsVo> myActiveDetails(Long id) {
        return ResultGenerator.success(activeService.myActiveDetails(id));
    }

    @ApiOperation(value = "删除活动")
    @ApiImplicitParam(name = "id", value = "活动id", paramType = "query", dataType = "Long")
    @Override
    public Result<Object> deleteActive(Long id, Long userId, Long businessId) {
        return ResultGenerator.success(activeService.deleteActive(id, userId, businessId));
    }

    @ApiOperation(value = "下架活动")
    @ApiImplicitParam(name = "id", value = "活动id", paramType = "query", dataType = "Long")
    @Override
    public Result<Object> takeDownActive(Long id, Long userId, Long businessId) {
        return ResultGenerator.success(activeService.takeDownActive(id, userId, businessId));
    }

    @ApiOperation(value = "首页活动列表")
    @ApiImplicitParam(name = "indexActivePageDto", value = "实体", paramType = "query", dataType = "IndexActivePageDto")
    @ApiResponse(response = IndexActivePageVo.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<Page<IndexActivePageVo>> pageIndexActive(@RequestBody IndexActivePageDto indexActivePageDto) {
        return ResultGenerator.success(activeService.pageIndexActive(indexActivePageDto));
    }

    @ApiOperation(value = "活动详情")
    @ApiImplicitParam(name = "id", value = "活动id", paramType = "query", dataType = "Long")
    @ApiResponse(response = ActiveDetailsVo.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<ActiveDetailsVo> details(Long id, Long userId, String shareCode) {
        return ResultGenerator.success(activeService.details(id, userId, shareCode));
    }

    @ApiOperation(value = "活动可用红包列表")
    @ApiImplicitParam(name = "id", value = "活动id", paramType = "query", dataType = "Long")
    @ApiResponse(response = ActiveRedPacketBilListVo.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<List<ActiveRedPacketBilListVo>> availableRedPacketList(Long id, Long userId) {
        return ResultGenerator.success(activeService.availableRedPacketList(id, userId));
    }

    @ApiOperation(value = "获取商品券的使用说明和可用商户")
    @ApiImplicitParam(name = "goodsUseDetailsDto", value = "实体", paramType = "body", dataType = "GoodsUseDetailsDto")
    @ApiResponse(response = ActiveGoodsUseDetailsVo.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<ActiveGoodsUseDetailsVo> useDetails(GoodsUseDetailsDto goodsUseDetailsDto) {
        return ResultGenerator.success(activeService.useDetails(goodsUseDetailsDto));
    }

    @ApiOperation(value = "后台活动列表")
    @ApiImplicitParam(name = "adminActivePageDto", value = "实体", paramType = "query", dataType = "AdminActivePageDto")
    @ApiResponse(response = AdminActivePageVo.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<Page<AdminActivePageVo>> pageAdminActive(AdminActivePageDto adminActivePageDto) {
        return ResultGenerator.success(activeService.pageAdminActive(adminActivePageDto));
    }

    @ApiOperation(value = "后台活动详情")
    @ApiImplicitParam(name = "id", value = "活动id", paramType = "query", dataType = "Long")
    @ApiResponse(response = AdminActivePageVo.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<AdminActivePageVo> adminDetails(Long id) {
        return ResultGenerator.success(activeService.adminDetails(id));
    }

    @Override
    public Result<Void> adminDelete(Long id) {
        activeService.adminDelete(id);
        return ResultGenerator.success();
    }

    @ApiOperation(value = "根据商家id查询活动总数")
    @ApiImplicitParam(name = "businessId", value = "商家id", paramType = "query", dataType = "Long")
    @ApiResponse(response = Integer.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<Integer> countByBusinessId(Long businessId) {
        return ResultGenerator.success(activeService.countByBusinessId(businessId));
    }

    @ApiOperation(value = "商家主页活动列表")
    @ApiImplicitParam(name = "businessHomeActivePageDto", value = "实体", paramType = "query", dataType = "BusinessHomeActivePageDto")
    @ApiResponse(response = BusinessHomeActivePageVo.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<Page<BusinessHomeActivePageVo>> pageHomeActive(BusinessHomeActivePageDto businessHomeActivePageDto) {
        return ResultGenerator.success(activeService.pageHomeActive(businessHomeActivePageDto));
    }

    @ApiOperation(value = "预分享活动")
    @ApiImplicitParam(name = "activePreShareDto", value = "实体", required = true, paramType = "body", dataType = "ActivePreShareDto")
    @ApiResponse(response = ActivePreShareVo.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<ActivePreShareVo> preShareActive(@RequestBody ActivePreShareDto activePreShareDto) {
        return ResultGenerator.success(activeService.preShareActive(activePreShareDto));
    }

    @ApiOperation(value = "分享活动")
    @ApiImplicitParam(name = "activeShareDto", value = "实体", required = true, paramType = "body", dataType = "ActiveShareDto")
    @ApiResponse(response = ActiveShareVo.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<ActiveShareVo> shareActive(@RequestBody ActiveShareDto activeShareDto) {
        return ResultGenerator.success(activeService.shareActive(activeShareDto));
    }

    @Override
    public Result<String> generateShortLink(ActiveShortLinkDto activeShortLinkDto) {
        return ResultGenerator.success(activeService.generateShortLink(activeShortLinkDto));
    }

    @ApiOperation(value = "根据条件查询活动列表")
    @ApiImplicitParam(name = "activeDto", value = "实体", paramType = "query", dataType = "ActiveDto")
    @ApiResponse(response = ActivePageVo.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<List<ActivePageVo>> listByDto(@RequestBody ActiveDto activeDto) {
        return ResultGenerator.success(activeService.listByDto(activeDto));
    }

    @ApiOperation(value = "根据活动id获取活动数据")
    @ApiImplicitParam(name = "id", value = "活动id", paramType = "query", dataType = "Long")
    @ApiResponse(response = ActiveVo.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<ActiveVo> getById(Long id) {
        return ResultGenerator.success(activeService.getActiveById(id));
    }

    @Override
    public Result<ActiveGroupBuyVo> getGroupBuyActiveByActiveId(Long activeId) {
        TbActiveGroupBuyPo tbActiveGroupBuyPo = activeGroupBuyService.getByActiveId(activeId);
        return ResultGenerator.success(BeanUtil.copyProperties(tbActiveGroupBuyPo, ActiveGroupBuyVo.class));
    }

    @ApiOperation(value = "扣减活动库存和商品活动冻结库存")
    @ApiImplicitParam(name = "activeSubStockDto", value = "实体", required = true, paramType = "body", dataType = "ActiveSubStockDto")
    @ApiResponse(response = Boolean.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<Boolean> subActiveStock(@RequestBody ActiveSubStockDto activeSubStockDto) {
        return ResultGenerator.success(activeService.subActiveStock(activeSubStockDto));
    }

    @ApiOperation(value = "增加活动库存和商品活动冻结库存，将红包退还，设置为未使用状态")
    @ApiImplicitParam(name = "activeAddStockDto", value = "实体", required = true, paramType = "body", dataType = "ActiveAddStockDto")
    @ApiResponse(response = Boolean.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<Boolean> addActiveStock(ActiveAddStockDto activeAddStockDto) {
        return ResultGenerator.success(activeService.addActiveStock(activeAddStockDto));
    }

    @ApiOperation(value = "根据活动id获取简约活动数据")
    @ApiImplicitParam(name = "id", value = "活动id", paramType = "query", dataType = "Long")
    @ApiResponse(response = ActiveSimpleVo.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<ActiveSimpleVo> getSimpleVoById(Long id) {
        return ResultGenerator.success(activeService.getSimpleVoById(id));
    }

    @ApiOperation(value = "扫描活动结束时间，如果已结束，修改活动状态")
    @Override
    public Result<Boolean> scanActiveJob() {
        return ResultGenerator.success(activeService.scanActiveJob());
    }

    @ApiOperation(value = "根据日期查询上新活动数")
    @Override
    public Result<Integer> getCountByDate(String startTime, String endTime) {
        return ResultGenerator.success(activeService.getCountByDate(startTime, endTime));
    }

    @ApiOperation(value = "更新活动状态")
    @Override
    public Result<Boolean> updateActiveState(@RequestBody UpdateActiveStateDto updateActiveStateDto) {
        return ResultGenerator.success(activeService.updateActiveState(updateActiveStateDto));
    }

    @Override
    public Result<Set<String>> listCityName() {
        return ResultGenerator.success(activeService.listCityName());
    }

    @Override
    public Result<List<SearchBusinessActiveVo>> listBusinessInProgressActives(List<Long> businessIds) {
        return ResultGenerator.success(activeService.listSearchBusinessActive(businessIds));
    }
}
